<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Pembayaran extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->m_member = new M_member();
        $this->m_kota = new M_kota();
        $this->m_provinsi = new M_provinsi();
        $this->m_mitra_kirim = new M_mitra_pengiriman();
        $this->m_tarip_kirim = new M_tarif_kirim();
        $this->m_pemesanan = new M_pemesanan();
        $this->m_pemesanan_det = new M_pemesanan_det();


        $this->load->library('cart');
    }

    function index($stat = NULL) {

        $id = $masuk = $this->session->userdata('id_member');

        if ($id == NULL || $id == "") {
            redirect("main/login_signup");
        }

        $data['title'] = 'Data Produk';

        $data['menu'] = array(
        );


        $data['title'] = " Simpan Pemesanan";

        $data['list_member'] = $this->m_member->where("id = $id")->get();
        $data['list_prov'] = $this->m_provinsi->get();
        $data['list_mitra'] = $this->m_mitra_kirim->get();
        $data['list_kota'] = $this->m_kota->search_data($data['list_member']->id_provinsi);

        $data['keranjang'] = $this->cart->contents();

        $data['isi'] = 'isi_pembayaran';
        $data['stat'] = $stat;

        $this->load->view('template_client', $data);
    }

    function save_transaksi() {

        $transaksi['id_member'] = $this->session->userdata('id_member');

        $transaksi['id'] = date('YmdHis') . $transaksi['id_member'];

        $transaksi['nama_depan'] = $this->input->post('nama_depan');
        $transaksi['nama_belakang'] = $this->input->post('nama_belakang');
        $transaksi['id_provinsi'] = $this->input->post('provinsi');
        $transaksi['id_kota'] = $this->input->post('kota');
        $transaksi['alamat'] = $this->input->post('alamat');
        $transaksi['kode_pos'] = $this->input->post('kode_pos');
        $transaksi['telp'] = $this->input->post('telp');

        $transaksi['nama_depan2'] = $this->input->post('nama_depan2');
        $transaksi['nama_belakang2'] = $this->input->post('nama_belakang2');
        $transaksi['id_provinsi2'] = $this->input->post('provinsi2');
        $transaksi['id_kota2'] = $this->input->post('kota2');
        $transaksi['alamat2'] = $this->input->post('alamat2');
        $transaksi['kode_pos2'] = $this->input->post('kode_pos2');
        $transaksi['telp2'] = $this->input->post('telp2');


        $transaksi['id_mitra_kirim'] = $this->input->post('mitra');
        $transaksi['jenis_pembayaran'] = $this->input->post('pembayaran');
        $transaksi['komentar'] = $this->input->post('komentar');
        $transaksi['date'] = date('Y-m-d H:i:s');
        $transaksi['status'] = 0;

        $data_biaya = $this->m_tarip_kirim->where("id_kota = " . $transaksi['id_kota2'] . " and id_mitra = " . $transaksi['id_mitra_kirim'])->get();
        $tujuan = $this->m_kota->where("id = " . $transaksi['id_kota2'])->get();
        $nama_kota = $tujuan->nama_kota;
        $biaya_kirim = $data_biaya->harga_kirim;
        $totberat = 0;
        $sub_total = 0;
        $grand = 0;
        $keranjang = $this->cart->contents();
        $n = 0;
        foreach ($keranjang as $dta) {
            $berat = $dta['options']['berat'];
            $harga = $dta['price'];
            $bnyk = $dta['qty'];
            $total = $harga * $bnyk;
            $diskon = $dta['options']['disc'];
            $totberat = ($berat * $bnyk) + $totberat;
            $sub = ($harga * $bnyk) - ($harga * $bnyk) * $diskon / 100;
            //sub total transaksi
            $sub_total = $sub + $sub_total;

            $transaksi_det[$n]['id_produk_det'] = $dta['id'];
            $transaksi_det[$n]['harga'] = $harga;
            $transaksi_det[$n]['bnyk'] = $bnyk;
            $transaksi_det[$n]['total'] = $total;
            $transaksi_det[$n]['disk'] = $diskon;
            $transaksi_det[$n]['sub_total'] = $sub;
            $transaksi_det[$n]['id_pemesanan'] = $transaksi['id'];
            $n++;
        }
        $total_biaya_kirim = ceil($totberat) * $biaya_kirim;
        $grand = $total_biaya_kirim + $sub_total;

        $transaksi['sub_total'] = $sub_total;
        $transaksi['biaya_kirim'] = $total_biaya_kirim;
        $transaksi['grand_total'] = $grand;

        //simpan transaksi
        $this->m_pemesanan->insert($transaksi);

        foreach ($transaksi_det as $td) {
            $this->m_pemesanan_det->insert($td);
        }

        $this->cart->destroy();

//        $this->index(1);
        redirect("main/member/view_history_order/13");
    }

}

?>
